import axios from 'axios'
import { Message } from 'element-ui'

axios.defaults.timeout = 5000

// 请求拦截
axios.interceptors.request.use(config => {
  // console.log(config)
  if (localStorage.token) {
    config.headers.Authorization = localStorage.token
  }
  return config
}, err => {
  Promise.reject(err)
})

// 响应拦截
axios.interceptors.response.use(response => {
  if (response.data.code === 200) {
    Message({
      message: response.data.msg,
      type: 'success'
    })
  } else {
    Message({
      message: response.data.msg,
      type: 'error'
    })
  }
  return response
}, err => {
  Promise.reject(err)
})


export default axios